/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package snip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Button;
import javafx.scene.control.CheckBox;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.DirectoryChooser;
import javafx.stage.FileChooser;
import javafx.stage.FileChooser.ExtensionFilter;
import javafx.stage.Stage;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import org.xml.sax.SAXException;

/**
 *
 * @author ian
 */
public class PopupController implements Initializable {
   
    //Icon and Generation Buttons
    @FXML Button button16;
    @FXML Button button32;
    @FXML Button buttonGen;
    
    //Textfields
    @FXML TextField displayNameText;
    @FXML TextField toolTipText;
    @FXML TextField classNameText;
    
    //Icon Images
    @FXML ImageView puzzle16;
    @FXML ImageView puzzle32;
    
    //Text Area
    @FXML TextArea codeTextArea;
    
    //Package Name
    @FXML ComboBox packageNameCombo;
    
    @FXML ComboBox categoryCB;
    @FXML Button   okButton;
    
    //Status messages.
    @FXML Label statusText;
    @FXML Label icon16Label;
    @FXML Label icon32Label;
    
    //Auto Install Boolean
    @FXML CheckBox autoInstallCB;
    Boolean auto;
    
    String selectedDIR;//This is the directory to store the generated files.
    String icon16NAME; //This is the user specified 16x16 icon name.
    String icon32NAME; //This is the user specified 32x32 icon name.
    String classNAME;  //This is the Class Name to use during generation.
    String packageNAME;//This is the palette package we are generating for.
    String codeNAME;   //This is the code snippet to generate.
    String toolNAME;   //This is the tooltip to use for the code snippet.
    String displayNAME;//This is the name that's displayed on the code palette.
    
    
    
    
    
    /**
     * Generation Button press action.  Starts source code generation.
     * 
     * @param event 
     */
    @FXML
    private void handleButtonAction(ActionEvent event) throws FileNotFoundException, IOException, URISyntaxException, ParserConfigurationException, SAXException, TransformerConfigurationException, TransformerException {
        System.out.println("Starting Source Generation.");
        
        //Make sure all fields have been filled out, none are empty and that the Class Name does NOT contain any spaces.
        if 
        ( 
             displayNameText.getText().isEmpty() | toolTipText.getText().isEmpty()       | classNameText.getText().isEmpty()             
             | 
             codeTextArea.getText().isEmpty()    | classNameText.getText().contains(" ") | packageNameCombo.getSelectionModel().isEmpty()
             |
             icon16NAME.isEmpty()                | icon32NAME.isEmpty()
        ) 
             {
             statusText.setText("Status: All fields must be filled out, and classes can't contain spaces.");
             } 
        
        else 
            
        {
            //Check if the user is specifing the default directory or if we are 
            //installing in a NetBeans package.
            if 
            (autoInstallCB.isSelected())
            {
                this.auto = true;
            }else{
                this.auto = false;
            }
    //Collect the user selections.
    this.classNAME = classNameText.getText();
    this.packageNAME = packageNameCombo.getSelectionModel().getSelectedItem().toString();
    this.codeNAME = codeTextArea.getText();
    this.toolNAME = toolTipText.getText();
    this.displayNAME = displayNameText.getText();
    
        //Get the output directory.
        System.out.println("Getting Save Directory.");
        
        //Choose Directory Popup Window
        DirectoryChooser chooser = new DirectoryChooser();
        if (auto){
        chooser.setTitle("Select NetBeans Package Name");    
        }else{
        chooser.setTitle("Source File Directory Chooser");
        }
        
        //Set root as default directory.
        File defaultDirectory = new File("\\");
        chooser.setInitialDirectory(defaultDirectory);
        
        File selectedDirectory = chooser.showDialog(new Stage().getOwner());
        
            //Make sure selected directory is not empty.
            if  (selectedDirectory != null)
             {
             System.out.println("Selected Directory is: " + selectedDirectory.toString());
             selectedDIR = selectedDirectory.toString();
             sourceGenerationStart();
             }
            else
             {
             statusText.setText("Status: No Directory Specified.");
             }
        
        
        
        }    
    }
    @FXML
    private void handleIcon16Action(ActionEvent event) throws FileNotFoundException {
        System.out.println("Getting 16x16 Icon.");
        
        //Choose Icon Popup
        FileChooser chooser = new FileChooser();
        chooser.setTitle("Choose an 16x16 .PNG Icon");
        
        File defaultDirectory = new File("\\");
        ExtensionFilter filters = new FileChooser.ExtensionFilter("Portable Network Graphics (*.png)", "*.png");
        chooser.getExtensionFilters().add(filters);
        chooser.setInitialDirectory(defaultDirectory);
        File selectedIcon = chooser.showOpenDialog(new Stage().getOwner());
        
        //Set icon imageview to new selection.
        if (selectedIcon.exists()){
        Image image = new Image(new FileInputStream(selectedIcon));
        puzzle16.setImage(image);
        icon16NAME = selectedIcon.getName();
        icon16Label.setText(icon16NAME);
        
        }else{
        statusText.setText("Status: Icon Does not Exist!");
        }
        
            
    }
    @FXML
    private void handleIcon32Action(ActionEvent event) throws FileNotFoundException {
        System.out.println("Getting 32x32 Icon.");
        
        //Choose Icon Popup
        FileChooser chooser = new FileChooser();
        chooser.setTitle("Icon Chooser");
        
        File defaultDirectory = new File("\\");
        ExtensionFilter filters = new FileChooser.ExtensionFilter("Portable Network Graphics (*.png)", "*.png");
        chooser.getExtensionFilters().add(filters);
        chooser.setInitialDirectory(defaultDirectory);
        File selectedIcon = chooser.showOpenDialog(new Stage().getOwner());
        
        //Set icon imageview to new selection.
        if (selectedIcon != null)
        {
        Image image = new Image(new FileInputStream(selectedIcon));
        puzzle32.setImage(image);
        icon32NAME = selectedIcon.getName();
        icon32Label.setText(icon32NAME);
        
        }else{
        statusText.setText("Status: Icon Does not Exist!");
        }
        
            
    }
    
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }    

    /**
     * Calls the source generation class TranslateTemplatetoSnippet to handle
     * all output.
     * 
     * @throws FileNotFoundException
     * @throws IOException
     * @throws URISyntaxException 
     */
    private void sourceGenerationStart() throws FileNotFoundException, IOException, URISyntaxException, ParserConfigurationException, SAXException, TransformerConfigurationException, TransformerException 
    {
        //Call Generation Class with all user specified attributes.
        
        TranslateTemplatetoSnippet translateTemplatetoSnippet = new TranslateTemplatetoSnippet(
                                     displayNAME, //This is the name that's displayed on the code palette.
                                     packageNAME, //This is the palette package we are generating for. 
                                       classNAME, //This is the Class Name to use during generation.
                                      icon16NAME, //This is the user specified 16x16 icon name.
                                      icon32NAME, //This is the user specified 32x32 icon name.
                                     selectedDIR, //This is the directory to store the generated files.
                                        codeNAME, //This is the code snippet to generate.
                                        toolNAME, //This is the tooltip to use for the code snippet.
                                            auto, //Determines if these sources are to be installed in a NetBeans Package or User supplied DIR.
                                            this);//Send a copy of this class.
                                     
    }
}
